const express = require('express');
const router = express.Router();
const { check } = require("express-validator");
const { validateRequest, customValidations } = require("../../middleware/validationMiddleware");
const dictController = require("../../controllers/dictController");

/**
 * @description: 查询所有字典数据
 * @return {*}
 */
router.get("/dict", dictController.getAllDicts);

/**
 * @description: 分页查询字典数据
 * @return {*}
 */
router.get(
  "/dict/page",
  [
    check("pageNum").exists().withMessage("pageNum参数缺失")
      .isInt({ min: 1 }).withMessage("pageNum必须为大于0的整数"),
    check("pageSize").exists().withMessage("pageSize参数缺失")
      .isInt({ min: 1, max: 100 }).withMessage("pageSize必须为1-100之间的整数"),
    check("status").optional().isIn([0, 1]).withMessage("status必须为0或1")
  ],
  validateRequest,
  dictController.getDicByPage
);

/**
 * @description: 根据ID查询字典详情
 * @return {*}
 */
router.get(
  "/dict/:id",
  [
    check("id").exists().withMessage("id参数缺失")
      .isInt({ min: 1 }).withMessage("id必须为大于0的整数")
  ],
  validateRequest,
  dictController.getDictById
);

router.post(
  "/dict",
  [
    check("name").exists().withMessage("name参数缺失")
      .custom(customValidations.isValidDictName).withMessage("字典名称格式不正确"),
    check("dictCode").exists().withMessage("dictCode参数缺失")
      .custom(customValidations.isValidDictCode).withMessage("字典编码格式不正确"),
    check("status").optional().isIn([0, 1]).withMessage("status必须为0或1")
  ],
  validateRequest,
  dictController.addDict
);

router.put(
  "/dict/:id",
  [
    check("id").exists().withMessage("id参数缺失")
      .isInt({ min: 1 }).withMessage("id必须为大于0的整数"),
    check("name").exists().withMessage("name参数缺失")
      .custom(customValidations.isValidDictName).withMessage("字典名称格式不正确"),
    check("dict_code").optional()
      .custom(customValidations.isValidDictCode).withMessage("字典编码格式不正确"),
    check("status").optional().isIn([0, 1]).withMessage("status必须为0或1")
  ],
  validateRequest,
  dictController.updateDict
);

router.delete(
  "/dict/:id",
  [
    check("id").exists().withMessage("id参数缺失")
     .isInt({ min: 1 }).withMessage("id必须为大于0的整数")
  ],
  validateRequest,
  dictController.deleteDict
);

module.exports = router;
